package com.fjwt.gz.proxy.ctrl.lite.official;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.OfficialReplyDTO;
import com.fjwt.gz.db.vo.OfficialReplyVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.OfficialMenuService;
import com.fjwt.gz.service.impl.OfficialReplyService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/officialReply")
public class OfficialReplyController extends CommonCtrl {

    @Autowired
    private OfficialReplyService replyService;

    @Autowired
    private OfficialMenuService menuService;

    /**
     * 获取素材列表
     */
    @PostMapping("/getMaterialList")
    public ApiRes getMaterialList()  {
        String appid = getValString("appid");
        String type = getValString("type");
        Integer begin = getValInteger("beginSize");
        Integer end = getValInteger("endSize");
        log.info("机构端获取素材列表接口调用，请求地址：/api/officialReply/getMaterialList，请求参数：appid={},type={},begin={},end={}", appid, type, begin, end);
        if (StringUtils.isEmpty(appid) || StringUtils.isEmpty(type) || null == begin || null == end) {
            return ApiRes.customFail("请求参数缺失！");
        }
        try {
            WxMpService wxService = menuService.setWxService(appid);
            WxMpMaterialFileBatchGetResult result = wxService.switchoverTo(appid).getMaterialService().materialFileBatchGet(type, begin, end);
            log.info("机构端获取素材列表接口调用，请求地址：/api/officialReply/getMaterialList，返回结果：{}", JSONObject.toJSONString(result));
            return ApiRes.ok(result);
        }catch (WxErrorException e){
            throw new BizException("获取素材列表失败！");
        }


    }

    /**
     * 获取回复列表
     */
    @PreAuthorize("hasAnyAuthority('ENT_MP_REPLY')")
    @PostMapping("/getList")
    public ApiRes getList() {
        OfficialReplyDTO dto = getObject(OfficialReplyDTO.class);
        log.info("机构端获取回复列表接口调用，请求地址：/api/officialReply/getList，请求参数：{}", JSONObject.toJSONString(dto));
        IPage<OfficialReplyVO> page=replyService.getPage(getIPage(),dto);
        log.info("机构端获取回复列表接口调用，请求地址：/api/officialReply/getList，返回结果：{}", JSONObject.toJSONString(page));
        return ApiRes.page(page);
    }

    /**
     * 新增回复消息
     */
    @PreAuthorize("hasAnyAuthority('ENT_MP_REPLY_ADD')")
    @PostMapping("/saveReply")
    public ApiRes saveReply() {
        Long sysUserId = getCurrentUser().getSysUserId();
        OfficialReplyDTO dto = getObject(OfficialReplyDTO.class);
        log.info("机构端新增回复消息接口调用，请求地址：/api/officialReply/saveReply，请求参数：{}", JSONObject.toJSONString(dto));
        replyService.insert(dto,  sysUserId);
        return ApiRes.ok();
    }

    /**
     * 获取回复详情
     */
    @PostMapping("/getReplyDetail")
    public ApiRes getReplyDetail() {
        Long replyId = getValLong("replyId");
        log.info("机构端获取回复详情接口调用，请求地址：/api/officialReply/getReplyDetail，请求参数：{}", JSONObject.toJSONString(replyId));
        OfficialReplyVO vo=replyService.getReplyDetail(replyId);
        log.info("机构端获取回复详情接口调用，请求地址：/api/officialReply/getReplyDetail，返回结果：{}", JSONObject.toJSONString(vo));
        return ApiRes.ok(vo);
    }

    /**
     * 修改回复消息
     */
    @PreAuthorize("hasAnyAuthority('ENT_MP_REPLY_EDIT')")
    @PostMapping("/updateReply")
    public ApiRes updateReply() {
        OfficialReplyDTO dto = getObject(OfficialReplyDTO.class);
        log.info("机构端修改回复消息接口调用，请求地址：/api/officialReply/updateReply，请求参数：{}", JSONObject.toJSONString(dto));
        replyService.updateReply(dto);
        return ApiRes.ok();
    }

    @PreAuthorize("hasAnyAuthority('ENT_MP_REPLY_DEL')")
    @PostMapping("/removeReply")
    public ApiRes removeReply() {
        Long replyId = getValLong("replyId");
        log.info("机构端删除回复消息接口调用，请求地址：/api/officialReply/removeReply，请求参数：{}", JSONObject.toJSONString(replyId));
        replyService.removeReply(replyId);
        return ApiRes.ok();
    }
}
